# -*- coding:utf-8 -*-
class Solution:
    # matrix类型为二维列表，需要返回列表
    def printMatrix(self, matrix):
        # write code
        res=[]
        if not matrix:
            return
        low = 0
        high = len(matrix)-1
        left = 0
        right = len(matrix[0])-1
        count = 0
        while low<=high and left<=right:
            count = count + 1
            print('left',left,'right',right,'loop',count)
            for i in range(left,right+1):
                res.append(matrix[low][i])
            for i in range(low+1,high+1):
                res.append(matrix[i][right])
            if low < high:
                for i in range(right-1,left+1):
                    res.append(matrix[high][i])
            if left < right:
                for i in range(high-1,low):
                    res.append(matrix[i][left])
            low = low + 1
            left = left + 1
            high = high - 1
            right = right - 1
        return res
so = Solution()
print(so.printMatrix([[1,2],[3,4]]))
'''
需要考虑特殊情况一行一列，一个等
if matrix没办法判断是否[]为空
matrix需要判断否则判断matrix[0]会报错
'''
# 直接pop操作
# -*- coding:utf-8 -*-
class Solution:
    # matrix类型为二维列表，需要返回列表
    def printMatrix(self, matrix):
        # write code
        res = []
        while matrix:
            # 打印首行
            '''
             因为res+和res.append()这两个不同，若 matrix.pop()为[]的话，res+存入[]后不会显示，
             对res的内容不影响，若用append的话，res中会显示有[]
            '''
            res += matrix.pop(0)
            # 首行没了还有，添加倒数第一列
            if matrix and matrix[0]:
                for row in matrix:
                    res.append(row.pop())
            # 打印最后一行
            if matrix:
                res += matrix.pop()[::-1]
            # 从下往上打印首列
            if matrix and matrix[0]:
                # 把列表顺序从上到下反转
                for row in matrix[::-1]:
                    res.append(row.pop(0))
        return res
